import {Component} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
import {IonicPage, NavController, ToastController} from 'ionic-angular';

import {User} from '../../providers/providers';
import {Api} from "../../providers/api/api";

@IonicPage()
@Component({
  selector: 'page-signup',
  templateUrl: 'signup.html'
})
export class SignupPage {


  user: { accountName: string, qq: string, password: string, mobile: string, code: string } = {
    accountName: '',
    qq: '',
    password: '',
    mobile: '',
    code: ''
  };

  codeValue = '获取验证码';

  status: boolean = false;

  // Our translated text strings
  private signupErrorString: string;
  private networkErrorString: string;

  constructor(public navCtrl: NavController,
              public User: User,
              public api: Api,
              public toastCtrl: ToastController,
              public translateService: TranslateService) {

    this.translateService.get('SIGNUP_ERROR').subscribe((value) => {
      this.signupErrorString = value;
    })
    this.translateService.get('NETWORK_ERROR').subscribe((value) => {
      this.networkErrorString = value;
    });

  }

  getCode() {
    var reg = /^([a-zA-Z0-9_-])+@(qq.com)+/;
    if (!reg.test(this.user.qq)) {
      return this.showToast("只能使用QQ邮箱！");
    }

    let reqParam = "?email=" + this.user.qq;
    let seq = this.api.get('utils/mailRegisterCode' + reqParam).share();

    seq.subscribe((res: any) => {
      // If the API returned a successful response, mark the user as logged in
      this.status = true;
      let timer = 60;
      let num = setInterval(() => {
        console.log(this.status, this.codeValue);
        if (timer == 0) {
          this.codeValue = '获取邮箱验证码';
          this.status = false;
          clearInterval(num);
          return;
        }
        timer--;
        this.codeValue = timer + "s后可重发";
      }, 1000)
    }, err => {
      this.showToast(err);
      console.error('ERROR', err);
    });
  }

  doSignup() {
    this.validate();

    let seq = this.api.post('customers', this.user).share();

    seq.subscribe((res: any) => {
      this.showToast("注册成功！");
      this.navCtrl.pop();
    }, err => {
      this.showToast(JSON.parse(err.error).status == 500 ? "验证码错误" : this.networkErrorString);
      console.error('ERROR', err);
    });
    return seq;
  }

  canSignUp() {
    if (this.user.accountName.length < 0) {
      return false;
    }
    if (this.user.qq.length < 0) {
      return false;
    }
    if (this.user.password.length < 6) {
      return false;
    }
    if (this.user.mobile.length != 11) {
      return false;
    }
    if (this.user.code.length != 4) {
      return false;
    }
    return true;
  }

  validate() {
    if (this.user.accountName.length < 0) {
      return this.showToast("用户名不能为空");
    }
    if (this.user.qq.length < 0) {
      return this.showToast("邮箱不能为空");
    }
    if (this.user.password.length < 6) {
      return this.showToast("密码不能少于6位");
    }
    if (this.user.mobile.length < 6) {
      return this.showToast("手机号格式不正确");
    }
    if (this.user.code.length != 4) {
      return this.showToast("验证码不正确");
    }
  }


  showToast(msg) {
    return this.toastCtrl.create({
      message: msg,
      duration: 1000,
      position: 'top'
    }).present();
  }

}
